package com.dkd.manage.controller;

import com.dkd.common.core.domain.R;
import com.dkd.manage.pojo.dto.PolicyQueryListDto;
import com.dkd.manage.pojo.entity.Policy;
import com.dkd.manage.result.PageResult;
import com.dkd.manage.service.PolicyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "策略管理相关接口")
@RequestMapping("/manage/policy")
public class PolicyController {

    @Autowired
    private PolicyService policyService;

    @ApiOperation("分页查询策略列表")
    @PreAuthorize("@ss.hasPermi('manage:policy:list')")
    @GetMapping("/list")
    public R<PageResult<Policy>> list(PolicyQueryListDto policyQueryListDto) {
        log.info("分页查询策略列表：{}", policyQueryListDto);
        PageResult<Policy> result = policyService.list(policyQueryListDto);
        return R.ok(result);
    }

    @ApiOperation("新增策略")
    @PreAuthorize("@ss.hasPermi('manage:policy:add')")
    @PostMapping
    public R<Integer> add(@RequestBody Policy policy) {
        log.info("新增策略：{}", policy);
        Integer rows = policyService.add(policy);
        return R.ok(rows);
    }

    @ApiOperation("根据id查询策略")
    @PreAuthorize("@ss.hasPermi('manage:policy:query')")
    @GetMapping("/{id}")
    public R<Policy> getPolicyById(@PathVariable Long id) {
        log.info("根据id查询策略：{}", id);
        Policy policy = policyService.getPolicyById(id);
        return R.ok(policy);
    }

    @ApiOperation("修改策略")
    @PreAuthorize("@ss.hasPermi('manage:policy:edit')")
    @PutMapping
    public R<Integer> edit(@RequestBody Policy policy) {
        log.info("修改策略：{}", policy);
        Integer rows = policyService.edit(policy);
        return R.ok(rows);
    }

    @ApiOperation("根据id删除策略")
    @PreAuthorize("@ss.hasPermi('manage:policy:remove')")
    @DeleteMapping("/{ids}")
    public R<Integer> deletePolicyById(@PathVariable Long[] ids) {
        log.info("根据id删除策略：{}", ids);
        Integer rows = policyService.deletePolicyById(ids);
        return R.ok(rows);
    }

}
